package com.cj.easypoi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.cj.easypoi.entity.DataArg;
import com.cj.easypoi.entity.Event;
import com.cj.easypoi.entity.Person;
import com.cj.easypoi.entity.SM;
import com.cj.easypoi.util.EasypoiUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.poi.ss.formula.functions.Even;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * ClassName: FileController <br/>
 * Function: 文件处理 <br/>
 * date: 2018/04/26 15:45 <br/>
 *
 * @author chenj
 * @version 1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {

    @PostMapping(value = "/excel/import")
    public void importExcel(@RequestParam("file") MultipartFile file) {
        List<Person> peoples = EasypoiUtil.importExcel(file, 1, 1,1,1, Person.class);
        System.out.println(peoples.toString());
    }

    @PostMapping(value = "/excel/import3")
    public void importExcelDVSV(@RequestParam("file") MultipartFile file) {
        List<DataArg> peoples = EasypoiUtil.importExcel(file, 1, 1,1,1, DataArg.class);
        System.out.println(peoples.toString());
    }



    @PostMapping(value = "/excel/import2")
    public void importExcel2(@RequestParam("file") MultipartFile file) {
        // startSheetNum 就是从哪一sheet页开始，第一sheet页就 是0     ;;  sheetNum 是读取1 sheet页的数据
        List<SM> peoples = EasypoiUtil.importExcel(file, 1, 1,1,1, SM.class);
        System.out.println(peoples.toString());
    }

    @GetMapping(value = "/excel/export")
    public void exportExcel(HttpServletResponse response) {
        //模拟从数据库获取需要导出的数据
//        List<Person> personList = new ArrayList<Person>();
//        Person person1 = new Person();
//        Person person2 = new Person();
//        Person person3 = new Person();
//        Person person4 = new Person();
//        person1.setFileId("1");
//        person2.setFileId("xm2");
//        person1.setFileName("daiang");
//        person1.setTags("谷向南是大老娘们");
//        person2.setTags("asdsadg");
//        personList.add(person1);
//        personList.add(person2);
//        personList.add(person3);
//        personList.add(person4);
//
//        //导出操作
//        FileUtil.exportExcel(personList,"花名册","422",Person.class,"虾米.xls",response);

//        TemplateExportParams templateExcel = new TemplateExportParams("static/easypoi-test.xlsx");
//        List<Map<String, String>> listMap = new ArrayList<>();
//        for (int i = 0; i < 100; i++) {
//            Map<String, String> lm = new HashMap<>();
//            lm.put("姓名", "tester" + i);
//            lm.put("性别", "男");
//            lm.put("生日", "2018-04-26");
//            listMap.add(lm);
//        }
//        Map<String, Object> dataMap = new HashMap<>();
//        dataMap.put("list", listMap);
//
//        Workbook workbook = ExcelExportUtil.exportExcel(templateExcel, dataMap);



            //模拟从数据库获取需要导出的数据
            List<Person> personList = new ArrayList<Person>();
            Person person1 = new Person();
            Person person2 = new Person();
            Person person3 = new Person();
            Person person4 = new Person();
            person1.setName("1");
            person2.setName("xm2");
            person3.setName("xm3");
            person4.setName("xm4");
            personList.add(person1);
            personList.add(person2);
            personList.add(person3);
            personList.add(person4);

            //导出操作
            FileUtil.exportExcel(personList,"花名册","422",Person.class,"虾米.xls",response);
        }
}
